package com.jz.algorithm.Stack;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Classname CQueue
 * @Description 用两个栈实现一个队列
 * @Date 2021/10/20 17:33
 * @Author Jz
 * @Version 1.0
 */
class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;


    public MyQueue() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }

    public void push(int x) {

        while (!stack2.empty()) {
            stack1.push(stack2.pop());
        }
        stack2.push(x);
        while (!stack1.empty()) {
            stack2.push(stack1.pop());
        }



    }

    public int pop() {

        return  stack2.pop();


    }

    public int peek() {

        return  stack2.peek();

    }

    public boolean empty() {

        return  stack2.empty();

    }
}

/**
 * @Classname MyQueue
 * @Description 请你仅使用两个队列实现一个后入先出（LIFO）的栈，并支持普通栈的全部四种操作（push、top、pop 和 empty）。
 * @Date 2021-11-26 9:46
 * @Author jz
 * @Version 
 */
class MyStack {
    LinkedList<Integer> list;
    LinkedList<Integer> list1;

    public MyStack() {
        list=new LinkedList();
        list1=new LinkedList();
    }

    public void push(int x) {

        while (list1.size() != 0) {
            list.addLast(list.removeFirst());
        }
        list.addFirst(x);
    }

    public int pop() {

       return list.pop();
    }

    public int top() {
        return  list.getFirst();

    }

    public boolean empty() {
        return  list.size()==0;

    }
}
